type EventHandler = (data: any) => void;

class EventBus {
    private events: Record<string, EventHandler[]> = {};

    on(event: string, callback: EventHandler): void {
        if (!this.events[event]) {
            this.events[event] = [];
        }
        this.events[event].push(callback);
    }

    off(event: string, callback: EventHandler): void {
        if (this.events[event]) {
            this.events[event] = this.events[event].filter(fn => fn !== callback);
        }
    }

    emit(event: string, data?: any): void {
        if (this.events[event]) {
            this.events[event].forEach(callback => {
                callback(data);
            });
        }
    }
}

const eventBus = new EventBus();

export default eventBus;